import { Vec3, math } from "cc";
import { PlayUser } from "../core/PlayUser";
import { WebSocketGame } from "./WebSocketGame";
import { EndReason, Game, HitBallAttribute } from "../core/Game";
import { Databus } from "../core/Databus";
import { NetHitMainBallFrame, NetKeyFrame } from "../core/WebSocketData";
const databus = Databus.Instance();
export class SendWebSocket {
  constructor() {
  }
  public header(oper: string, roomId: string, userId: string, gameId: string, gameType: number, battleCate: number) {
    return {
      oper, roomId: roomId, userId: userId, gameId: gameId, gameType: gameType, battleCate: battleCate
    }
  }
  public heartRequst() {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('heartbeat', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: {
        userId: userId,
      }
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  public roomCreated(playUser: PlayUser) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('room', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: {
        playUser: playUser,
      }
    }
    console.log('send create room ', userId, cg.roomId);
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  public userStatus(playUser: PlayUser) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('user', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: playUser,
    }
    console.log('send user status ', userId, cg.roomId, playUser);
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  // 白球被击打瞬间的信息: 白球信息 + 球杆信息 + 当前帧信息
  public sendMainBallHitted(mframe: NetHitMainBallFrame) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('mainballhitted', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: mframe,
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  public sendAskConfirm(step: number, askType: number, askSeq: number) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    console.log('send ask confirm ', step, askType, askSeq);
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('askconfirm', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: {
        step: step,
        askType: askType,
        askSeq: askSeq,
      },
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  // 发送第1帧，对齐球的号码和位置
  public sendGameFirstFrame(frame: any) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('gamefirstframe', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: frame,
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  // 发送关键帧数据
  public sendKeyFrame(frame: NetKeyFrame) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('keyframe', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: frame,
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  // 同步当前步结束的信息
  public sendSyncStepOver(data: any) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    console.log('send sync step over ', data);
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('syncstepover', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: data,
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }

  public sendEndGame(endReason: EndReason) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    console.log('send end game ', endReason);
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('over', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: {
        userId: endReason.userId, // reason 针对当前userId用户
        reason: endReason.reason,
      },
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  // 自由球时，同步白球移动的位置
  public sendMainBallPosition(seq: number, pos: Vec3) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const step = Game.Instance().currentStep;
    const body = {
      header: this.header('mainballfree', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: {
        step: step,
        seq: seq,
        position: {
          x: pos.x, y: pos.y, z: pos.z
        }
      },
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }

  // 瞄准时，球杆所在的位置和方向
  public sendAimLine(seq: number, ep: Vec3, dp: Vec3, ro: math.Quat) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const step = Game.Instance().currentStep;
    const body = {
      header: this.header('aimline', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: {
        step: step,
        seq: seq,
        endPoint: { x: ep.x, y: ep.y, z: ep.z },
        defaultPoint: { x: dp.x, y: dp.y, z: dp.z },
        qiuganRotation: { x: ro.x, y: ro.y, z: ro.z, w: ro.w }
      },
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
  // 聊天内容
  public sendChatContent(cType: number, content: string) {
    if (!Game.Instance().battleCate.isFrientBattle()) return;
    const userId = databus.selfInfo.userId;
    const cg = Game.Instance().websocketHeader();
    const body = {
      header: this.header('chat', cg.roomId, userId, cg.gameId, cg.gameType, cg.battleCate),
      data: {
        cType: cType,
        content: content,
      },
    }
    const msg = JSON.stringify(body);
    WebSocketGame.Instance().sendSocketMessage(msg);
  }
}